package com.maple.controller.admin;

import com.maple.constant.AutoFillConstant;
import com.maple.constant.JwtClaimsConstant;
import com.maple.dto.EmployeeDTO;
import com.maple.dto.EmployeeLoginDTO;
import com.maple.dto.EmployeePageQueryDTO;
import com.maple.dto.EmployeePasswordDTO;
import com.maple.entity.Employee;
import com.maple.properties.JwtProperties;
import com.maple.result.PageResult;
import com.maple.result.Result;
import com.maple.service.EmployeeService;
import com.maple.utils.JwtUtil;
import com.maple.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    JwtProperties jwtProperties;

    /**
     * 员工登陆
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        log.info("用户登陆：{}",employeeLoginDTO.getUsername());
        EmployeeLoginVO employeeLoginVO = employeeService.login(employeeLoginDTO);

        // 生成令牌
        Map<String, Object> claim = new HashMap<>();
        claim.put(JwtClaimsConstant.EMP_ID, employeeLoginVO.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claim);

        // 封装对象
        employeeLoginVO.setToken(token);

        return Result.success(employeeLoginVO);
    }


    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @PostMapping
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工：{}",employeeDTO.getName());
        employeeService.save(employeeDTO);

        return Result.success();
    }

    /**
     * 员工分页查询
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(EmployeePageQueryDTO pageQueryDTO){
        log.info("员工分页查询");
        PageResult pageResult = employeeService.pageQuery(pageQueryDTO);

        return Result.success(pageResult);
    }


    /**
     * 员工状态修改
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result empStatus(@PathVariable String status, String id){
        employeeService.empStatus(status, id);
        return Result.success();

    }

    /**
     * 通过id查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> queryById(@PathVariable String id){
        Employee emp =  employeeService.queryById(id);
        return Result.success(emp);
    }


    /**
     * 编辑员工信息
     * @param
     * @return
     */
    @PutMapping
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        employeeService.update(employeeDTO);
        return Result.success();
    }


    @PutMapping("/editPassword")
    public Result changePassword(@RequestBody EmployeePasswordDTO employeePasswordDTO){
        employeeService.changePassword(employeePasswordDTO);
        return Result.success();
    }

}
